<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    /**
     * 
     * Create facilities
     * 
     */
    class Facilities {

        public function facilitesFor($classStrparse, $parser, $objFields, $attributes, $group_ID, $lang) {
            $facililies = NULL;

            $fields = unserialize($objFields);

            // Do oparation for each ATTRIBUTES
            foreach ($attributes as $attribute):

                $title = $classStrparse->find_lang_in_string($attribute['attribute_name'], $lang);
                $title_en = $classStrparse->find_lang_in_string($attribute['attribute_name'], 'en');

                $name = format_input_name($title);
                $name_en = format_input_name($title_en);

                //$this->dump ( $title . " --- " . $name );

                $fieldsKey = $group_ID . '___' . $name_en;

                // Replace "" with ''  
                if (isset($fields[$fieldsKey]))
                    $fields[$fieldsKey] = str_replace("\"", "'", $fields[$fieldsKey]);

                // Set in facility array
                if (isset($fields[$fieldsKey]) && $fields[$fieldsKey] != '')
                    $this->parser($parser, $facililies, $attribute, $title, $name_en, $classStrparse->find_lang_in_string($fields[$fieldsKey], $lang));



            endforeach;

            return $facililies;
        }

        public function parser($parserType, &$facililies, $attribute, $title, $name_en, $value = FALSE) {
            switch ($parserType) {
                case 'FOR_FILTER':
                    $this->parseForFilter($facililies, $attribute, $title, $name_en);
                    break;

                case 'FOR_HOTEL':
                    $this->parseForHotel($facililies, $attribute, $title, $value);
                    break;

                case 'FOR_POLICES':
                    $this->parseForPolices($facililies, $attribute, $title, $value);
                    break;
                
                case 'FOR_WEBSITE':
                    $this->parseForWebiste($facililies, $attribute, $title, $value);
                    break;
            }
        }

        private function parseForFilter(&$facililies, $attribute, $title, $name_en) {
            switch ($attribute['attribute_type']) {

                case 'yes/no':
                    $facililies[$name_en] = $title;
                    break;
            }
        }

        private function parseForWebiste(&$facililies, $attribute, $title, $value) {
            switch ($attribute['attribute_type']) {

                case 'descriptive data':
                case 'info':
                case 'numeric':
                case 'area':
                case 'distance':
                case 'yes/no':
                    $facililies[$title] = $value;
                    break;
            }
        }

        private function parseForHotel(&$facililies, $attribute, $title, $value) {
            switch ($attribute['attribute_type']) {
                case 'descriptive data':
                case 'info':
                    $facililies .= $title . ' ' . $value . ', ';
                    break;

                case 'yes/no':
                    $facililies .= $title . ', ';
                    break;

                case 'numeric':
                    $facililies .= $title . ' ' . $value . ', ';
                    break;

                case 'area':
                    $facililies .= $title . ' ' . $value . ', ';
                    break;

                case 'distance':
                    $unit = 'm';
                    if ((int) $value > 1000):
                        $unit = 'km';
                        $value = (int) $value / 1000;
                    endif;
                    $facililies .= $title . ' ' . $value . $unit . ', ';
                    break;
            }
        }

        private function parseForPolices(&$facililies, $attribute, $title, $value) {
            switch ($attribute['attribute_type']) {
                case 'descriptive data':
                case 'info':
                    if ($title == 'From' || $title == 'To')
                        $facililies .= $value . ', ';
                    else
                        $facililies .= $title . ' ' . $value . ', ';
                    break;

                case 'yes/no':
                    $facililies .= $title . ', ';
                    break;
            }
        }

    }
    